package com.leo.foodmaster.admin.foodMaterial.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.foodMaterial.entity.FoodMaterial;
import com.leo.foodmaster.admin.foodMaterial.service.IFoodMaterialService;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.util.ExcelUtil;
import com.leo.foodmaster.commons.util.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-07-14
 */
@RestController
@AllArgsConstructor
@Api(tags = "食材管理")
@RequestMapping("/foodMaterial")
public class FoodMaterialController {
    private final IFoodMaterialService foodMaterialService;

    @ApiOperation("条件分页查询食材列表")
    @GetMapping("/list")
    public Result<IPage<FoodMaterial>> getFoodMaterialPageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "食材加工阶段（1：毛菜，2：净菜，3：熟食）") @RequestParam(required = false) String processPhase,
            @ApiParam(value = "食材名称") @RequestParam(required = false) String name,
            @ApiParam(value = "食材品牌") @RequestParam(required = false) String brand,
            @ApiParam(value = "食材类型Id") @RequestParam(required = false) Long typeId) {
        IPage<FoodMaterial> pageList  = foodMaterialService
                .getFoodMaterialPageList(new Page<>(pageNum, pageSize), processPhase, name, brand, typeId);
        return Result.success(pageList);
    }


    @PostMapping
    @ApiOperation("新增食材")
    public Result<Boolean> addFoodMaterial(@RequestBody FoodMaterial foodMaterial) {
        Long organId = WebUtils.getUser().getOrganId();
        foodMaterial.setOrganId(organId);
        boolean save = foodMaterialService.save(foodMaterial);
        return Result.success(save);
    }

    @PutMapping
    @ApiOperation("修改食材")
    public Result<Boolean> updateFoodMaterial(@RequestBody FoodMaterial foodMaterial) {
        boolean update = foodMaterialService.updateById(foodMaterial);
        return Result.success(update);
    }

    @PutMapping("/updateStatus")
    @ApiOperation("修改食材状态")
    public Result<Object> updateStatus(@RequestBody FoodMaterial foodMaterial) {
        boolean update = foodMaterialService.updateById(foodMaterial);
        return Result.success(update);
    }

    @DeleteMapping("/{foodMaterialIds}")
    @ApiOperation("删除食材")
    public Result<Object> deleteFoodMaterialType(@ApiParam("食材ids") @PathVariable String foodMaterialIds) {
        return foodMaterialService.deleteFoodMaterial(foodMaterialIds);
    }

    @GetMapping("/downloadTemplate")
    @ApiOperation("下载食材模板")
    public Result<Object> downloadTemplate() {
        List<FoodMaterial> list = new ArrayList<>();
        ExcelUtil<FoodMaterial> util = new ExcelUtil<>(FoodMaterial.class);
        return util.exportExcel(list, "食材模板");
    }

    @GetMapping("/export")
    @ApiOperation("导出食材数据")
    public Result<Object> export(@ApiParam(value = "食材加工阶段（1：毛菜，2：净菜，3：熟食）") @RequestParam(required = false) String processPhase,
                                 @ApiParam(value = "食材名称") @RequestParam(required = false) String name,
                                 @ApiParam(value = "食材品牌") @RequestParam(required = false) String brand,
                                 @ApiParam(value = "食材类型Id") @RequestParam(required = false) Long typeId) {
        List<FoodMaterial> list  = foodMaterialService
                .getFoodMaterialList(processPhase, name, brand, typeId);
        ExcelUtil<FoodMaterial> util = new ExcelUtil<>(FoodMaterial.class);
        return util.exportExcel(list, "食材数据");
    }

    @PostMapping("/import")
    @ApiOperation("导入食材数据")
    public Result<Object> importFoodMaterial(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<FoodMaterial> util = new ExcelUtil<>(FoodMaterial.class);
        List<FoodMaterial> foodMaterialList = util.importExcel(file.getInputStream());
        String userName = WebUtils.getUser().getName();
        return foodMaterialService.importFoodMaterial(foodMaterialList, updateSupport, userName);
    }


}

